package com.nhjf.model.system;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.GenericGenerator;

import com.nhjf.command.system.CreateRoleCommand;
import com.nhjf.command.system.ModifyRoleCommand;
import com.nhjf.model.BaseModel;


/**
 * 
 * @类功能说明：角色
 *
 */
@Entity
@Table(name = "role")
@SuppressWarnings("serial")
public class Role extends BaseModel{
	
	/**
	 * 主键
	 */
	@Id
	@GeneratedValue(generator = "generator")
	@GenericGenerator(name = "generator", strategy = "native")
	@Column(name = "id", unique = true)
	private Long id;
	
	/**
	 * 角色名称
	 */
	@Column(name = "name")
	private String name;
	
	/**
	 * 角色编码
	 */
	@Column(name = "number")
	private String number;
	
	/**
	 * 备注
	 */
	@Column(name = "remark")
	private String remark;
	
	
	/**
	 * 创建时间
	 */
	@Column(name = "createDate")
	private Date createDate;
	
	/**
	 * 角色下的所有用户
	 */
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "user_has_role" , joinColumns = { @JoinColumn(name = "role_id") }, inverseJoinColumns = { @JoinColumn(name = "user_id") })
	private Set<User> userList = new LinkedHashSet<User>();
	
	/**
	 * 角色拥有的权限资源
	 */
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "role_has_res" , joinColumns = { @JoinColumn(name = "role_id") }, inverseJoinColumns = { @JoinColumn(name = "resource_id") })
	private Set<Resource> resourceList = new LinkedHashSet<Resource>();

	public void createRole(CreateRoleCommand command){
		setName(command.getName()); 
		setNumber(command.getNumber());
		setRemark(command.getRemark());
		for(Long resourceId:command.getResourceIdList()){
			Resource resource = new Resource();
			resource.setId(resourceId);
			resourceList.add(resource);
		}
		setCreateDate(new Date());
		setCreateDate(new Date());
	}
	
	public Set<User> getUserList() {
		return userList;
	}

	public void setUserList(Set<User> userList) {
		this.userList = userList;
	}

	public void modifyRole(ModifyRoleCommand command){
		setName(command.getName());
		setNumber(command.getNumber());
		setRemark(command.getRemark());
		resourceList.clear();
		for(Long resourceId:command.getResourceIdList()){
			Resource resource = new Resource();
			resource.setId(resourceId);
			resourceList.add(resource);
		}
		
	}

	public Long getId() {
		return id;
	}



	public void setId(Long id) {
		this.id = id;
	}



	public String getName() {
		return name;
	}



	public void setName(String name) {
		this.name = name;
	}



	public String getNumber() {
		return number;
	}



	public void setNumber(String number) {
		this.number = number;
	}



	public String getRemark() {
		return remark;
	}



	public void setRemark(String remark) {
		this.remark = remark;
	}



	public Date getCreateDate() {
		return createDate;
	}



	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}



	public Set<Resource> getResourceList() {
		return resourceList;
	}



	public void setResourceList(Set<Resource> resourceList) {
		this.resourceList = resourceList;
	}
	


	
	
	
	
	
	

}
